package com.ruoyi.carUser.controller;

import com.ruoyi.carUser.domain.UserMerchant;
import com.ruoyi.carUser.service.UserService;
import com.ruoyi.carUser.service.WashCarService;
import com.ruoyi.carUser.util.JwtUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.domain.OrderEvaluation;
import com.ruoyi.system.api.domain.ServiceDetails;
import com.ruoyi.system.api.domain.User;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class WashCarController {
    @Autowired
    private WashCarService washCarService;
    @Autowired
    private UserService userService;

    @RequestMapping("getMerchant")
    public R getMerchant(UserMerchant userMerchant){
        List<UserMerchant> userMerchants = washCarService.selectUserMerchantList(userMerchant);
//        if (userMerchants.size()==0){
//            return AjaxResult.error("未获取到商户信息");
//        }
        return R.ok(userMerchants);
    }
//获取商铺的服务
    @RequestMapping("getServiceDetails")
    public R getServiceDetail(UserMerchant userMerchant){
        List<ServiceDetails> serviceDetails = washCarService.getServiceDetails(userMerchant);
        return R.ok(serviceDetails);
    }
//    获取该服务对应的评价信息
    @RequestMapping("getEvaluation")
    public R getEvaluation(UserMerchant userMerchant){
        List<OrderEvaluation> evaluationList = washCarService.getEvaluationList(userMerchant);
        return R.ok(evaluationList);
    }
    //获取当前用户的信息和默认车辆
    @RequestMapping("getUserInfo")
    public  R getUserInfo(Long carId){
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)attributes;
        HttpServletRequest request = sra.getRequest();
        //2 从请求头获取token
        String token = request.getHeader("token");
        Claims claims = null;
        try {
             claims = JwtUtil.parseJWT(token);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
       Integer userId =  Integer.parseInt(claims.getSubject());
        System.out.println(userId);
        User user = userService.selectUserByUserId(userId,carId);
        return R.ok(user);
    }




}
